<template>
  <div>
    <head-container></head-container>
    <div class="match_waper">
      <!-- 赛事列表 -->
      <section v-if="!id" class="match_item" @click="goToInfo(item.id)" v-for="item in matchList" :key="item.id">
        <div class="match_img">
          <img :src="item.image" alt="图片加载失败">
        </div>
        <div class="match_info">
          <h2>{{item.game}}</h2>
          <div class="author_wrap">
            <div class="author">发布人<span>{{item.desc}}</span></div>
            <div class="timer">起止时间<span>{{GLOBAL.timestampToTime(item.start)}}</span> 至 <span>{{GLOBAL.timestampToTime(item.end)}}</span></div>
          </div>'
          <div class="sign_up">
            <span>正在报名</span>
          </div>
        </div>
      </section>
      <!-- 赛事详情页 -->
      <section v-if="id" class="match_info_wrap">
        <el-carousel>
          <el-carousel-item class="match_banner" v-for="(item,i) in matchInfo.img" :key="i">
            <img :src="item.url" alt="">
          </el-carousel-item>
        </el-carousel>
        <p class="pos">
          当前位置：
          <router-link to="/" tag="span">首页</router-link>>
          <router-link to="/match" tag="span">赛事活动</router-link>>
          <span>{{matchInfo.detail?matchInfo.detail.game:''}}</span>
        </p>
        <div class="match_main">
          <h2>{{matchInfo.detail?matchInfo.detail.game:''}}</h2>
          <div class="author_wrap">
            <div class="author">发布人<span>{{matchInfo.detail?matchInfo.detail.desc:''}}</span></div>
            <div class="timer">起止时间<span>{{matchInfo.detail?GLOBAL.timestampToTime(matchInfo.detail.start):''}}</span> 至 
            <span>{{matchInfo.detail?GLOBAL.timestampToTime(matchInfo.detail.end):''}}</span></div>
          </div>
          <section class="detail" v-html="matchInfo.detail?matchInfo.detail.detail:''"></section>
          <div class="join">
            <span class="join_btn" @click="joinMatch"><span>我要报名：</span>
            <span class="price">{{matchInfo.detail?matchInfo.detail.price:''}}</span></span>
          </div>
        </div>
        <el-dialog
          title="请选择参加人员"
          :visible.sync="dialogVisible"
          width="42%"
          min-width='270px'
          class="study_info_wrap"
          :before-close="handleClose">
          <div @click="changeActive(index,item.id)" class="user_item" :class="{active:activeIndex==index}" v-for="(item,index) in studyInfo" :key="item.id">
            <p>{{item.name}}</p>
            <p>性别：{{item.sex}}</p>
            <p>出生日期：{{item.birth}}</p>
            <p>身份证号：{{GLOBAL.hideCard(item.cardId)}}</p>
          </div>
          <div v-if="status" style="text-align:center;">
            获取学员信息中，请等待...
          </div>
          <div v-if="!status&&!studyInfo.length">没有获取到您的信息,可以点击
            <span class="goto_add" @click="goToAdd">这里</span>添加信息
          </div>
          <span slot="footer" class="dialog-footer">
            <el-checkbox v-model="checked">是否需要购买保险</el-checkbox>
            <el-button @click="goToAdd">添加信息</el-button>
            <el-button :loading ="loading" type="primary" @click="ok">确 定</el-button>
          </span>
        </el-dialog>
        <el-dialog
          title="请使用微信扫描下方二维码付款"
          :visible.sync="dialogVisible1"
          width="30%"
          class="wx_pay_wrap"
          :before-close="handleClose">
          <p>保险：{{payInfo.safe}}</p>
          <p>报名费：{{payInfo.price}}</p>
          <p>应付总价：{{payInfo.total}}</p>
          <img v-if="payInfo.url" :src="payInfo.url" alt="">
        </el-dialog>
      </section>
    </div>
    <foot-container></foot-container>
  </div>
</template>
<script>
import headContainer from '../components/headContainer'
import footContainer from '../components/footContainer'
export default {
  components:{
    headContainer,
    footContainer
  },
  data(){
    return{
      matchList:[],
      id:this.$route.query.id,
      matchInfo:{},
      dialogVisible:false,
      dialogVisible1:false,
      studyInfo:[],
      choiceId:'',
      activeIndex:0,
      activeID:'',
      checked:true,
      payInfo:{},
      loading:false,
      resGo:true,
      status:true
    }
  },
  watch:{
    $route(){
      this.id = this.$route.query.id
      if (this.id) {
        this.getInfo()
      }else{
        this.getList()
      }
    }
  },
  methods:{
    changeActive(index,id){
      this.activeIndex = index;
      this.activeID = id
    },
    ok(){
      if (!this.id||!this.activeID) {
        return
      }
      this.loading = true;
      this.GLOBAL.tokenRequest({
        method:'post',
        baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
        url:'pays/joins',
        data:{
          game_id:this.id,
          student:this.activeID,
          safe:this.checked?1:''
        }
      }).then(({data:data})=>{
        this.dialogVisible = false;
        this.loading = false;
        if (data.status) {
          this.dialogVisible1 = true;
          this.payInfo = data;
          this.monitorPay(data.joinid)
        }
      })
    },
    monitorPay(joinid){//建立轮询返回true付款成功
      this.GLOBAL.request({
        method:'post',
        baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
        url:'games/res',
        data:{
          joinid
        }
      }).then(({data:data})=>{
        if (!data.status&&this.resGo) {//没付款
          this.monitorPay(joinid)
        }
        if (data.status==1) {//成功
          this.dialogVisible1 = false;
          this.$message({
            type:'success',
            message:data.message
          })
        }
        if (data.status==2) {//超时
          this.dialogVisible1 = false;
          this.$message({
            type:'error',
            message:data.message
          })
        }
      })
    },
    joinMatch(){
      this.GLOBAL.tokenRequest({
          method:"post",
          baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
          url:'students/show',
        }).then(({data:data})=>{
          this.status = false;
          this.studyInfo = data;
          this.activeID = data[0]?data[0].id:'';
        })
      this.dialogVisible = true;
    },
    goToInfo(id){
      this.$router.push({path:'/match',query:{id}})
    },
    goToAdd(){
       this.GLOBAL.authLogin('user')
    },
    getList(){
      this.GLOBAL.request({
        baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
        url:'games/show'
      }).then(({data:data})=>{
        this.matchList = data;
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭吗？')
        .then(_ => {
          done();
          this.resGo = false;
          this.loading = false;
        })
        .catch(_ => {});
    },
    getInfo(){
      this.GLOBAL.request({
        method:'post',
        baseURL:this.GLOBAL.RNTY_SHOP_MALL_URL,
        url:'games/detail',
        data:{
          id:this.id
        }
      }).then(({data:data})=>{
        this.matchInfo = data;
      })
    },
  },
  mounted(){
    if (!this.id) {
      this.getList();
    }else{
      this.getInfo();
    }
  }
}
</script>
<style scoped>
  .match_waper{
    width:1200px;
    margin:0 auto;
    min-height: 680px;
    padding-top:50px;
    box-sizing: border-box;
  }
  .match_item{
    display: flex;
    padding:14px 0;
    margin-bottom:10px;
    justify-content: space-between;
    align-items: center;
    background-color: #f3f3f3;
    cursor: pointer;
  }
  .match_img{
    width:280px;
    height:160px;
  }
  .match_img img{
    width:100%;
    height:100%;
  }
  .match_info{
    width:860px;
  }
  .match_info h2{
    font:24px/40px "微软雅黑";
  }
  .match_info .author_wrap{
    display: flex;
    margin-top:30px;
  }
  .author_wrap div{
    margin-right:50px;
    position: relative;
    padding-left:20px;
  }
  .author_wrap div::after{
    position: absolute;
    top:2px;
    left:0;
  }
  .author_wrap .author::after{
    content: url('../assets/images/author.png')
  }
  .author_wrap .timer::after{
    content: url('../assets/images/timer.png')
  }
  .author_wrap span{
    color:#adadad;
    margin: 0 10px;
    font:14px/16px "微软雅黑";
  }
  .sign_up{
    text-align: right;
    padding-right:50px;
  }
  .sign_up span{
    background-color: #027dc6;
    color:#fff;
    font:20px/34px "微软雅黑";
    padding:0 8px;
    border-radius: 5px;
  }
  .match_banner img{
    width:100%;
    height:100%;
  }
  .pos{
    margin: 10px 0;
  }
  .pos span{
    cursor: pointer;
  }
  .match_main h2{
    text-align: center;
    font:20px/50px "微软雅黑";
  }
  .match_main .author_wrap{
    text-align: center;
  }
  .match_main .author_wrap div{
    display: inline-block;
  }
  .detail{
    margin-top:30px;
    line-height: 24px;
    text-indent: 26px;
  }
  .join{
    margin:30px 0;
    text-align: right;
  }
  .join_btn{
    font:24px/30px "微软雅黑";
    cursor: pointer;
  }
  .user_item{
    padding:5px;
    box-sizing: border-box;
    width:230px;
    height:105px;
    border:1px solid #73caff;
    position: relative;
    display: inline-block;
    margin-right:10px;
    margin-bottom:10px;
    cursor: pointer;
  }
  .active::after{
    position: absolute;
    content: '√';
    width:25px;
    height:25px;
    bottom: 0;
    right:0;
    text-align: center;
    font-family: Arial, Helvetica, sans-serif;
    color:#fff;
    background-color: #73caff;
  }
  .goto_add{
    padding:0 10px;
    font-size:20px;
    color:#73caff;
    cursor: pointer;
    text-decoration: underline;
  }
  .wx_pay_wrap{
    text-align: center;
  }
  .wx_pay_wrap img{
    width:80%;
  }
</style>

